package works;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * @author tzp
 * @since 2020/10/22
 */
public class X {
    public static void main(String[] args) throws Exception {
        String base = "/Users/tzp/Desktop/out/";
        String baso = "/Users/tzp/Desktop/ouo/";
        String x = "daas_app_waste_resources.csv";
        BufferedReader br = new BufferedReader(new FileReader(base + x));
        BufferedWriter bw = new BufferedWriter(new FileWriter(baso + x));
        String in = br.readLine();
        while ((in = br.readLine()) != null) {
            if (in.contains("\"")) {
                int i1 = in.indexOf("\"");
                int i2 = in.lastIndexOf("\"");
                in = in.substring(0, i1) +
                        in.substring(i1 + 1, i2).replace(",", "").replace("\"", "") +
                        in.substring(i2 + 1);
            }

            String[] split = in.split(",");
            if (split.length != 14) {
                throw new RuntimeException();
            }

            String time = split[split.length - 1];
            split[split.length - 1] = String.valueOf(timeToHour(time));

            String gb = split[11];
            split[11] = gb.replace(" GB", "");

            String name = split[1];
            name = name.replaceAll("-", "");
            name = name.replaceAll("\\d", "");
            name = name.substring(0, Math.min(15, name.length()));

            bw.write(name + ",");


            String join = String.join(",", split);
            bw.write(join + "\n");
        }

        br.close();
        bw.flush();
        bw.close();
    }

    public static float timeToHour(String time) {
        //1 hr 5 min 20 sec
        time = time.trim();
        String[] ts = time.split(" ");
        float hour = 0;
        if (ts.length == 6) {
            hour = hour + Integer.parseInt(ts[0]);
            hour = hour + Integer.parseInt(ts[2]) / 60.0f;
        } else if (ts.length == 4) {
            hour = hour + Integer.parseInt(ts[0]) / 60.0f + 1 / 60.0f;
        } else if (ts.length == 2) {
            hour = 1 / 60.0f;
        } else {
            throw new RuntimeException();
        }
        return hour;
    }
}
